import json
from datetime import datetime
from flask import Blueprint, g, render_template, session, current_app

from flask_mobility.decorators import mobilized
from .database import db
from .auth import login_required
from .crud import Timetables, Users
from .router import to_route

bp = Blueprint('professor', __name__, url_prefix='/professor')

def strftime(t: datetime):
    return t.strftime( '%Y-%m-%d %H:%M:%S')  

@bp.before_app_request
def load_logged_in_user():
    user_id = session.get('user_id')
    if user_id is None:
        g.user = None
    else:
        g.user = Users.get_user(db.session, user_id)

@bp.route('/')
@login_required
def professor():
    timetables = Timetables.get_timetables(db.session, g.user.id)
    context = {
        'user': g.user,
        'strftime': strftime,
        'timetables': timetables,
        'school': current_app.config.get('SCHOOL_NAME')
    }

    return to_route('professor/index.html', **context)
